
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <title>VDR-STREAMER-Video-Convert</title>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

 </head>
    <body>
	
<script>

var statuscolormap = {};

function server_name(){
	var url = window.location.href;
	var index = url.lastIndexOf("/");
	if(index<0)
	{
		return url;
	}
	return url.substring(0,index);
}

function epocToHuman(epoc)
{
	var date = new Date( parseFloat(epoc) *1000 );
	return date.toLocaleDateString() + " " + date.toLocaleTimeString();
}


function changeBackgroundBack(idObj)
{
		document.getElementById(idObj.id).style.background = statuscolormap[idObj.id];
}
function changeItBackground(idObj)
{
		document.getElementById(idObj.id).style.background = "BLUE";
}

function dummy(cc)
{
	//alert("DUMMY");
}
function doAction(idObj)
{

	if(document.getElementById("status_" + idObj.id).innerHTML=="WAITING" ||
	document.getElementById("status_" + idObj.id).innerHTML=="ONGOING")
	{
		var r=confirm("Do you really want to cancel?");
		if(r)
		{
			$.get("convert.run?stop=" + idObj.id, null);
			$.get("videolist.txt",updateStatus);
			document.getElementById(idObj.id).style.background = 'WHITE';
			statuscolormap[idObj.id]="WHITE";
		}
	}
	else
	{
		if(document.getElementById("status_" + idObj.id).innerHTML=="DONE")
		{
			var r=confirm("Hi dude, Do you really want to do this again???");
			if(!r)
			{
				return;
			}
		}

		$.get("convert.run?convert=" + idObj.id, dummy);
		document.getElementById(idObj.id).style.background = 'YELLOW';
		statuscolormap[idObj.id]="YELLOW";
		//$.get("videolist.txt",updateStatus);
		//alert(idObj.id);
	}
}

function updateStatus(data)
{
	var lines = data.split("\n");
	
	for(var i=0;i<lines.length;i++)
	{
		var params = lines[i].split(";");
		if(params.length<5) continue;

		var info = params[1].split(" ");

		
		var date = epocToHuman(info[1]);
		var length = info[2]/60;
		var channel = params[0];
		var name = params[2];
		var description = params[3];
		var id = params[4];
		var status = params[5];

	
		document.getElementById("status_" + id).innerHTML = status;
		if(status=="ONGOING" || status=="WAITING" || status=="STOPPING")
		{
			statuscolormap[id]="YELLOW";
			document.getElementById(id).style.background = 'YELLOW';
			
		}
		if(status=="DONE")
		{
			document.getElementById(id).style.background = 'GREEN';		
			statuscolormap[id]="GREEN";
		}
		if(status=="IDLE")
		{
			document.getElementById(id).style.background = 'WHITE';		
			statuscolormap[id]="WHITE";
		}
	}
	document.write("</table>");
}

function printVideos(data)
{
	document.write( "Server: " + server_name() + "<br>");
	document.write("<a href=\"index.html\">index.html</a><br>");
	document.write("<a href=\"converted_videos.html\">Converted videos</a><br>");

	var lines = data.split("\n");
	
	document.write("<table border=\"0.5\">");
	for(var i=0;i<lines.length;i++)
	{
		var params = lines[i].split(";");
		if(params.length<5) continue;
		var info = params[1].split(" ");
		
		var date = epocToHuman(info[1]);
		var length = info[2]/60;
		var channel = params[0];
		var name = params[2];
		var description = params[3];
		var id = params[4];
		var status = params[5];

		//statuscolormap[id] = "WHITE";
		statuscolormap[id]="WHITE";
		document.write('<tr id="' + id + '" title="' + description + '" onclick="doAction(this)" ' + 
		' onmouseover="changeItBackground(this)" onmouseout="changeBackgroundBack(this)">');
		document.write( "<td>" + date + "<td>\n");
		document.write( "<td>" + name + " [" + length + "]<td>\n");
		
		document.write("<td id=status_" + id + ">" + status + "</td>");
		document.write("</tr>");
	}
	document.write("</table>");
}

function update()
{
  $.get("videolist.txt",updateStatus);
  setTimeout(update, 5000);
}

$.get("videolist.txt",printVideos);
update();

</script>
 </body>
</html>

